package com.imti.service.impl;

import com.imti.bean.DeskInfo;
import com.imti.bean.Doctor;
import com.imti.bean.Nurse;
import com.imti.bean.pojo.NdutyVo;
import com.imti.mapper.NdutyVoDao;
import com.imti.service.NdutyService;
import com.imti.util.PageBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NdutyServiceImpl implements NdutyService {

    @Autowired
    private NdutyVoDao ndutyVoDao;

    @Override
    public PageBean pageDuty(Map<String, Object> map) {
        // 1、查询总条数
        NdutyVo ndutyVo = (NdutyVo) map.get("ndutyVo");
        PageBean pageBean = (PageBean) map.get("page");
        Integer pageLimt = (pageBean.getPage() == null) ? 5 : pageBean.getPage();
        Integer currNum = (pageBean.getCurrNo() == null) ? 1 : pageBean.getCurrNo();
        int totalCount = ndutyVoDao.ndutyConut(ndutyVo);
        // 2、查询列表
        PageBean page = new PageBean(pageLimt, currNum);
        map.put("page", page);
        List<NdutyVo> list = ndutyVoDao.selectAll(map);
        // 3、封装pagebean
        PageBean npage = new PageBean(pageLimt, totalCount, currNum, list, "/nduty/page", dutyParams(ndutyVo));
        return npage;
    }
    private String dutyParams(NdutyVo ndutyVo) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(ndutyVo.getNdutyDate())) {
            sb.append("&ndutyDate=").append(ndutyVo.getNdutyDate());
        }
        if (StringUtils.isNotBlank(ndutyVo.getNurseName())) {
            sb.append("&nurseName=").append(ndutyVo.getNurseName());
        }
        return sb.toString();
    }

    /*添加数据*/
    @Override
    public int insert(NdutyVo ndutyVo) {
        return ndutyVoDao.insertAll(ndutyVo);
    }

    /*查询护士*/
    @Override
    public List<Nurse> fillAllNurse() {
        return ndutyVoDao.select();
    }
    /*查询科室*/
    @Override
    public List<DeskInfo> findAllDesk() {
        return null;
    }


    /* 根据id查询值班信息*/
    @Override
    public List<NdutyVo> selectBy(NdutyVo ndutyId) {
        return ndutyVoDao.selectOne(ndutyId);
    }

    /*根据ndutyId修改数据*/
    @Override
    public int updateById(NdutyVo ndutyVo) {
        return ndutyVoDao.update(ndutyVo);
    }



}
